/*
* blog.cpp - This file is the part of the wp-notifier.(code name "luan luan")
* Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#include "blog.h"
#include "commentsmodel.h"
#include "common.h"

QString encodePassword(const QString &pass, const QString &key)
{
	QString result;
	int n1, n2;

	if(key.length() == 0)
		return pass;

	for(n1 = 0, n2 = 0; n1 < pass.length(); ++n1) {
		ushort x = pass.at(n1).unicode() ^ key.at(n2++).unicode();
		QString hex;
		hex.sprintf("%04x", x);
		result += hex;
		if(n2 >= key.length())
			n2 = 0;
	}
	return result;
}

QString decodePassword(const QString &pass, const QString &key)
{
	QString result;
	int n1, n2;

	if(key.length() == 0)
		return pass;

	for(n1 = 0, n2 = 0; n1 < pass.length(); n1 += 4) {
		ushort x = 0;
		if(n1 + 4 > pass.length())
			break;
		x += QString(pass.at(n1)).toInt(NULL,16)*4096;
		x += QString(pass.at(n1+1)).toInt(NULL,16)*256;
		x += QString(pass.at(n1+2)).toInt(NULL,16)*16;
		x += QString(pass.at(n1+3)).toInt(NULL,16);
		QChar c(x ^ key.at(n2++).unicode());
		result += c;
		if(n2 >= key.length())
			n2 = 0;
	}
	return result;
}



BlogData::BlogData() 
{
	m_blog_id = 0;
	m_is_admin = false;
}

BlogData::BlogData(const BlogData& other) : QSharedData(other)
{
	m_blog_id = other.m_blog_id;
	m_url = other.m_url;
	m_blog_name = other.m_blog_name;
	m_username = other.m_username;
	m_password = other.m_password;
	m_xmlrpc_url = other.m_xmlrpc_url;
	m_is_admin = other.m_is_admin;

}
void BlogData::fromXml(const QDomElement& el)
{
	readEntry(el, "url", &m_url);
	readNumEntry(el, "blog_id", &m_blog_id);
	readEntry(el, "blog_name", &m_blog_name);
	readEntry(el, "username", &m_username);
	QString pass;
	readEntry(el, "password", &pass);
	m_password =  decodePassword(pass, m_url);
	readEntry(el, "xmlrpc_url", &m_xmlrpc_url);
	readBoolEntry(el, "is_admin", &m_is_admin);
}

void BlogData::toXml(QDomElement& el) const
{
	QDomDocument doc = el.ownerDocument();
	el.appendChild( textTag(doc, "url", m_url));
	el.appendChild( textTag(doc, "blog_id", QString::number(m_blog_id)));
	el.appendChild( textTag(doc, "blog_name", m_blog_name));
	el.appendChild( textTag(doc, "username", m_username));
	el.appendChild( textTag(doc, "password", encodePassword(m_password, m_url)));
	el.appendChild( textTag(doc, "xmlrpc_url", m_xmlrpc_url));
	el.appendChild( textTag(doc, "is_admin", m_is_admin? "true" : "false"));
}

Blog::Blog()
{
	d = new BlogData();
	//for (int i = 0; i < _countof(m_commentCount); ++i) {
	//	m_commentCount[i] = 0;
	//}
	///*m_blog_id = 0;
	//m_is_admin = false;*/
	////m_nApprove = m_nHold = m_nSpam = 0;
	//m_nLastId = 0;
	//m_lastTimeStamp = QDateTime::currentDateTime();
	//m_model = new CommentsModel(this);
}

Blog::Blog(const Blog& other) : d(other.d)
{

}

Blog::~Blog()
{
	/*delete m_model;
	m_model = NULL;*/
}

//Blog& Blog::operator=(const Blog& other)
//{
//	*((BlogData*)(this)) = *((BlogData*)(&other));
//	for (int i = 0; i < _countof(m_commentCount); ++i) {
//		m_commentCount[i] = other.m_commentCount[i];
//	}
//	m_lastTimeStamp = other.m_lastTimeStamp;
//	m_nLastId = other.m_nLastId;
//	return *this;
//}



//void Blog::fromXml(const QDomElement& el)
//{
//	readEntry(el, "url", &m_url);
//	readNumEntry(el, "blog_id", &m_blog_id);
//	readEntry(el, "blog_name", &m_blog_name);
//	readEntry(el, "username", &m_username);
//	QString pass;
//	readEntry(el, "password", &pass);
//	m_password =  decodePassword(pass, m_url);
//	readEntry(el, "xmlrpc_url", &m_xmlrpc_url);
//	readBoolEntry(el, "is_admin", &m_is_admin);
//
//	readNumEntry(el, "approve", &m_commentCount[CommentData::approve]);
//	readNumEntry(el, "hold", &m_commentCount[CommentData::hold]);
//	readNumEntry(el, "spam", &m_commentCount[CommentData::spam]);
//	readNumEntry(el, "lastcommentid", &m_nLastId);
//	QString datetime;
//	readEntry(el, "lastcommentdatetime", &datetime);
//	m_lastTimeStamp = QDateTime::fromString(datetime, Qt::ISODate);
//
//}
//
//QDomElement Blog::toXml(QDomDocument *doc)const
//{
//	QDomElement el = doc->createElement("blog");
//	el.appendChild( textTag(*doc, "url", m_url));
//	el.appendChild( textTag(*doc, "blog_id", QString::number(m_blog_id)));
//	el.appendChild( textTag(*doc, "blog_name", m_blog_name));
//	el.appendChild( textTag(*doc, "username", m_username));
//	el.appendChild( textTag(*doc, "password", encodePassword(m_password, m_url)));
//	el.appendChild( textTag(*doc, "xmlrpc_url", m_xmlrpc_url));
//	el.appendChild( textTag(*doc, "is_admin", m_is_admin? "true" : "false"));
//
//	el.appendChild( textTag(*doc, "approve", QString::number(m_commentCount[CommentData::approve])));
//	el.appendChild( textTag(*doc, "hold", QString::number(m_commentCount[CommentData::hold])));
//	el.appendChild( textTag(*doc, "spam", QString::number(m_commentCount[CommentData::spam])));
//	el.appendChild( textTag(*doc, "lastcommentid", QString::number(m_nLastId)));
//	el.appendChild( textTag(*doc, "lastcommentdatetime", m_lastTimeStamp.toString(Qt::ISODate)));
//	return el;
//}


//void Blog::setCount(CommentData::CommentStatus s, int value)
//{
//	
//}





//void Blog::getCommentsFinished(bool)
//{
//	wpGetAllComments* cmd = (wpGetAllComments*)sender();
//	m_comments = cmd->m_comments;
//	foreach(Comment c, cmd->m_comments) {
//		if (c.dateCreated() > m_lastTimeStamp) {
//			m_lastTimeStamp = c.dateCreated();
//		}
//		if (c.id() > m_nLastId) {
//			m_nLastId = c.id();
//		}
//	}
//	emit commentsRefreshed();
//}
//void Blog::xmlRpcFinished(bool success)
//{
//	wpCommand* command = static_cast<wpCommand*>(sender());
//	if (!success)
//		return;
//	if(command->type() == wpCommand::GetCommentsCount) {
//		wpGetCommentCount* cmd = (wpGetCommentCount*)command;
//		m_nApprove = cmd->approved;
//		m_nHold = cmd->awaiting_moderation;
//		m_nSpam = cmd->spam;
//		wpGetAllComments* getComments = new wpGetAllComments(this, this);
//		getComments->execute(0, s_opt.m_pageSize);
//		connect(getComments, SIGNAL(finished(bool)), SLOT(getCommentsFinished(bool)));
//		/*for (int i = 0; i <= CommentData::spam; ++i) {
//			wpGetComments* getComments = new wpGetComments(*this, wpGetComments::FetchMore, this);
//			getComments->execute(0, Comment::statusToString((CommentStatus)i), 0, 1);
//			connect(getComments, SIGNAL(finished(bool)), SLOT(xmlRpcFinished(bool)));
//		}*/
//	}
//	//else if (command->type() == wpCommand::GetComments) {
//	//	wpGetComments* cmd = (wpGetComments*)command;
//	//	/*QFile file(homeDir() + "/" + cmd->m_status + ".xml");
//	//	if (file.open(QIODevice::WriteOnly)) {
//	//		file.write(command->rawResponse());
//	//	}*/
//
//	//	m_comments = cmd->m_comments;
//	//	foreach(Comment c, cmd->m_comments) {
//	//		if (c.dateCreated() > m_lastTimeStamp) {
//	//			m_lastTimeStamp = c.dateCreated();
//	//		}
//	//		if (c.id() > m_nLastId) {
//	//			m_nLastId = c.id();
//	//		}
//	//	}
//	//	emit commentsRefreshed();
//	//}
//}

